﻿using System;
using System.Collections.Generic;

namespace DashFire
{
  public class ResUpdateMessage
  {
    public enum MT // MessageType
    {
      Log,  //日志
      Error,//错误
      DownLoadFileCount,  //下载文件数量
      DownLoadFile, //下载文件
      DecompressFile, //解压文件
      Finish,       //更新完成
      Message,      //更新信息
    }

    public delegate void Listen(MT MessageType, int nParam, string strParam);

    public void SetError(ResUpdateError err, string reason = null)
    {
      mError = err;
      if (Listener != null)
        Listener(MT.Error, (int)err, reason);

    }
    public void Log(string format, params object[] args)
    {
      if (Listener != null)
        Listener(MT.Log, int.MinValue, string.Format(format, args) );
    }

    public string GetErrorInfo()
    {
      string info = string.Empty;
      if (mError == ResUpdateError.Network_Error)
      {
        info = Dict.Get("StrDictionary_String_27");
        if (string.IsNullOrEmpty(info))
        {
          info = "网络连接失败！";
        }

      }
      else
      {
        string infoFormat = Dict.Get("StrDictionary_String_28");
        if (string.IsNullOrEmpty(infoFormat))
        {
          infoFormat = "更新失败，请稍后重试！\n失败原因（{0}）";
        }
        info = string.Format(infoFormat, (int)mError);
      }
      return info;
    }

    public void SendMessage(MT MessageType, int IntParam = 0, string StringParam = null)
    {
      if (Listener != null)
        Listener(MessageType, IntParam, StringParam);
    }

    public void SendFinishMessage(int param)
    {
      if (Listener != null)
        Listener(MT.Finish, param, string.Empty);
    }

    public void SendDownLoadMessage(string file)
    {
      if (Listener != null)
        Listener(MT.DownLoadFile, 0, file);
    }
    public void DeCompressMessage(string file)
    {
      if (Listener != null)
        Listener(MT.DecompressFile, 0, file);
    }

    #region 单例
    public static ResUpdateMessage Instance { get { return sInstance; } }
    private static ResUpdateMessage sInstance = new ResUpdateMessage();
    private ResUpdateMessage() { }
    #endregion

    public Listen Listener
    {
      set { sListener = value; }
      get { return sListener; }
    }
    private Listen sListener = null;
    private ResUpdateError mError = ResUpdateError.None;
  }
}
